package com.mr.productclient.controller;

import com.mr.productclient.entity.ProductCategory;
import com.mr.productclient.entity.ProductInfo;
import com.mr.productclient.service.CategoryService;
import com.mr.productclient.service.ProductService;
import com.mr.productclient.utils.ResultVOUtil;
import com.mr.productclient.vo.CartVO;
import com.mr.productclient.vo.ProductVO;
import com.mr.productclient.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Class: ProductController
 * User (作者):MRui
 * TODO (描述)：商品Controller
 * Date 2018-11-03 14:13
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    /**
    * User (作者): MRui
    * TODO (描述)：查询所有在架的商品
    * Data：2018-11-03 14:14
    */
    @PostMapping("/list")
    public ResultVO list(){
        //1.查询所有上架商品
        List<ProductInfo> productInfoList = productService.getUpAll(0);

        //2.查询类目（一次性查询）
        //List<Integer> categoryTypeList = new ArrayList<>();
        //lambda表达式  将productInfoList先转成stream然后取出CategoryType组成一个list
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);
        //3.数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory : productCategoryList) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());
            List<ProductInfo> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo:productInfoList) {
                if (productInfo.getCategoryType()==productCategory.getCategoryType()){
                    ProductInfo productInfoVO = new ProductInfo();
                    //Spring 提供复制工具将productInfo对象复制到productInfoVO里面
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoList(productInfoVOList);
            productVOList.add(productVO);
        }

        ResultVO resultVO = ResultVOUtil.success(productVOList);

        return resultVO;
    }

    /**
    * User (作者): MRui
    * TODO (描述)：根据ids获取商品（订单调用api接口）
    * Data：2018-11-04 17:45
    */
    @PostMapping("/findList")
    public List<ProductInfo> findList(@RequestBody List<String> ids){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return productService.findListByIdIn(ids);
    }
    /**
    * User (作者): MRui
    * TODO (描述)：配合hystrix实现demo（使用RestTemplate方式服务之间调用时用，feign方式集成了hystrix）
    * Data：2018-11-18 17:37
    */
    @PostMapping("/findListForHystrix")
    public List<ProductInfo> findListForHystrix(@RequestBody List<String> ids){
        /*try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return productService.findListByIdIn(ids);
    }

    /**
    * User (作者): MRui
    * TODO (描述)：减库存订单调用api接口）
    * Data：2018-11-04 21:48
    */
    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<CartVO> cartVOList){
        productService.decreaseStock(cartVOList);
    }

    @GetMapping("/test")
    public String test(){
        String a = "this is a test";
       // ResultVO resultVO = ResultVOUtil.success(a);
        return a;
    }
}
